﻿using System;
using System.Linq;
using ComponentFactory.Krypton.Toolkit;
using Xeml.Document.Contracts;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Xid.Controls.XemlStore
{
    public partial class LoadFromXemlStoreFrm : KryptonForm
    {
        string connectionString;

        public string SelectedXeml { get; private set; }
        public string SelectedId { get; private set; }

        public LoadFromXemlStoreFrm(string connectionString)
        {
            InitializeComponent();
            this.connectionString = connectionString;
            SelectedXeml = string.Empty;
            SelectedId = string.Empty;
        }

        private void LoadFromXemlStoreFrm_Shown(object sender, EventArgs e)
        {
            using (SqlCommand cmd = new SqlCommand())
            using (cmd.Connection = new SqlConnection(connectionString))
            {
                cmd.CommandText = @"SELECT Id, Name, Description, ExperimenterLastName, StartDate, DATE as [date saved], XEML FROM xeml.[Document] order by [date saved] desc";
                cmd.CommandType = System.Data.CommandType.Text;

                try
                {
                    cmd.Connection.Open();
                    cmd.Transaction = cmd.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);

                    System.Windows.Forms.BindingSource bs = new System.Windows.Forms.BindingSource();
                    System.Data.DataTable dt = new System.Data.DataTable();
                    dt.Load(cmd.ExecuteReader());
                    bs.DataSource = dt;
                    bs.ResetBindings(true);

                    kryptonDataGridViewDocumentBrowser.DataSource = bs;
                    kryptonDataGridViewDocumentBrowser.Refresh();

                    kryptonDataGridViewDocumentBrowser.Columns["Id"].Visible = false;
                    kryptonDataGridViewDocumentBrowser.Columns["XEML"].Visible = false;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }

        private void kryptonButtonOk_Click(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = kryptonDataGridViewDocumentBrowser.SelectedRows[0];
            SelectedId = currentRow.Cells[kryptonDataGridViewDocumentBrowser.Columns["Id"].Index].Value.ToString();
            SelectedXeml = currentRow.Cells[kryptonDataGridViewDocumentBrowser.Columns["XEML"].Index].Value.ToString();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void kryptonButtonCancel_Click(object sender, EventArgs e)
        {
            SelectedId = string.Empty;
            SelectedXeml = string.Empty;
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        public static string LoadXemlFromStore(Guid XemlId, string connectionString)
        {
            using (SqlCommand cmd = new SqlCommand())
            using (cmd.Connection = new SqlConnection(connectionString))
            {
                cmd.CommandText = @"SELECT XEML FROM xeml.[Document] WHERE XemlId = @XemlId";
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@XemlId", XemlId);

                try
                {
                    cmd.Connection.Open();
                    cmd.Transaction = cmd.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
                    using (SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection | System.Data.CommandBehavior.SequentialAccess | System.Data.CommandBehavior.SingleResult | System.Data.CommandBehavior.SingleRow))
                    {
                        reader.Read();
                        return reader.GetString(0);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }
    }
}
